package com.park.web.listener;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.park.common.empty.EmptyUtils;

/**
 * license验证
 */
//@WebListener
public class LicenseListener implements ServletContextListener {

	private static final long MAX_TIME = 1000*60*60*24*365L;
	private static final Logger  logger = LoggerFactory.getLogger(LicenseListener.class);
	private static final String LICENSE_PATH = "/license.lc";
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		try {
			String path = sce.getServletContext().getContextPath()+LICENSE_PATH;
			FileInputStream fis = new FileInputStream(new File(path));
			List<String> readLines = IOUtils.readLines(fis);
			if(EmptyUtils.isEmpty(readLines)){
				logger.info("license不存在。。。");
				Thread.sleep(MAX_TIME);//License不存在，则阻止程序继续运行下去
			}
		} catch (Exception e) {
			try {
				Thread.sleep(MAX_TIME);
			} catch (Exception e1) {
				logger.error("license不存在",e1);
			}
		}
	}

}
